package org.yaac.server.egql.evaluator;

import static com.google.common.collect.Sets.newHashSet;

import java.util.Set;

import org.yaac.server.egql.EGQLUtil;
import org.yaac.server.egql.EntityProperty;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;



/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class FieldEvaluator extends Evaluator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * @deprecated evaluator text = property name
	 */
	@Deprecated
	private final EntityProperty field;
	
	/**
	 * input can be fieldname only, like 'abc', or with alias 'abc.abc'
	 * 
	 * @param input
	 */
	public FieldEvaluator(String input) {
		super();
		this.setText(input);
		this.field = EGQLUtil.parseEntityProperty(input);
	}
	
	/**
	 * @param field
	 */
	public FieldEvaluator(EntityProperty field) {
		super();
		this.field = field;
	}

	public String getEntityAlias() {
		return field.getEntityName();
	}

	public String getFieldName() {
		return field.getPropertyName();
	}

	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		return record.lookup(this.getText());
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((field == null) ? 0 : field.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		FieldEvaluator other = (FieldEvaluator) obj;
		if (field == null) {
			if (other.field != null)
				return false;
		} else if (!field.equals(other.field))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "FieldEvaluator [field=" + field + "]";
	}

	@Override
	public Set<AggregationEvaluator> aggregationChildren() {
		return newHashSet();
	}

	@Override
	public Set<EntityProperty> nonAggregationProperties() {
		return newHashSet(this.field);
	}

	@Override
	public void validate() throws EGQLException {		
	}
}
